// Cookie 工具
!function (e) {
    var n = !1;
    if ("function" == typeof define && define.amd && (define(e), n = !0), "object" == typeof exports && (module.exports = e(), n = !0), !n) {
        var o = window.Cookies, t = window.Cookies = e();
        t.noConflict = function () {
            return window.Cookies = o, t
        }
    }
}(function () {
    function e() {
        for (var e = 0, n = {}; e < arguments.length; e++) {
            var o = arguments[e];
            for (var t in o) n[t] = o[t]
        }
        return n
    }

    function n(o) {
        function t(n, r, i) {
            var c;
            if ("undefined" != typeof document) {
                if (arguments.length > 1) {
                    if ("number" == typeof(i = e({path: "/"}, t.defaults, i)).expires) {
                        var a = new Date;
                        a.setMilliseconds(a.getMilliseconds() + 864e5 * i.expires), i.expires = a
                    }
                    i.expires = i.expires ? i.expires.toUTCString() : "";
                    try {
                        c = JSON.stringify(r), /^[\{\[]/.test(c) && (r = c)
                    } catch (e) {
                    }
                    r = o.write ? o.write(r, n) : encodeURIComponent(String(r)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent), n = (n = (n = encodeURIComponent(String(n))).replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)).replace(/[\(\)]/g, escape);
                    var s = "";
                    for (var f in i) i[f] && (s += "; " + f, !0 !== i[f] && (s += "=" + i[f]));
                    return document.cookie = n + "=" + r + s
                }
                n || (c = {});
                for (var p = document.cookie ? document.cookie.split("; ") : [], d = /(%[0-9A-Z]{2})+/g, u = 0; u < p.length; u++) {
                    var l = p[u].split("="), C = l.slice(1).join("=");
                    this.json || '"' !== C.charAt(0) || (C = C.slice(1, -1));
                    try {
                        var g = l[0].replace(d, decodeURIComponent);
                        if (C = o.read ? o.read(C, g) : o(C, g) || C.replace(d, decodeURIComponent), this.json) try {
                            C = JSON.parse(C)
                        } catch (e) {
                        }
                        if (n === g) {
                            c = C;
                            break
                        }
                        n || (c[g] = C)
                    } catch (e) {
                    }
                }
                return c
            }
        }

        return t.set = t, t.get = function (e) {
            return t.call(t, e)
        }, t.getJSON = function () {
            return t.apply({json: !0}, [].slice.call(arguments))
        }, t.defaults = {}, t.remove = function (n, o) {
            t(n, "", e(o, {expires: -1}))
        }, t.withConverter = n, t
    }

    return n(function () {
    })
});

const myVueObj = new Vue({});

// axios 拦截器
var instance = axios.create({
    baseURL: "", // api的base_url
    timeout: 65000 // 请求超时时间
})
var requestCount = 0;
var fullLoading;
// request拦截器
instance.interceptors.request.use(
    config => {
        var cookieValue = Cookies.get("vkey");
        if (stringUtil.isNotEmpty(cookieValue)) {
            config.headers['Authorization'] = 'Bearer ' + cookieValue;// 让每个请求携带自定义token 请根据实际情况自行修改
        }
        requestCount++;
        fullLoading = myVueObj.$loading({
            lock: true,
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0)',
            customClass: 'font-white'
        });
        return config
    },
    error => {
        console.log(error); // for debug
        Promise.reject(error);
    })


// respone拦截器
instance.interceptors.response.use(
    response => {
        requestCount--;
        if (requestCount === 0) {
            fullLoading.close();
        }
        const res = response.data;
        if (res.status !== 0 && res.status !== 101) {
            if (res.status === 1) {
                myVueObj.$message({
                    message: res.msg,
                    type: 'warning',
                    duration: 5 * 1000
                })
            } else {
                myVueObj.$message({
                    message: res.msg,
                    type: 'error',
                    duration: 5 * 1000
                })
            }
            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    error => {
        requestCount--;
        if (requestCount === 0) {
            fullLoading.close();
        }
        var errorMsg = error.message;
        if (error.message === 'Network Error') {
            errorMsg = "网络异常";
        }
        myVueObj.$message({
            message: errorMsg,
            type: 'error',
            duration: 5 * 1000
        })

        return Promise.reject(error)
    }
)


var currentVule = {
    currentSceneId: ''
}
// 生成UUID
function getUUID() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);

    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}


